<?php

namespace app\common\model\system;

use app\common\model\BaseModel;

class Config extends BaseModel
{
    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [
        'value' => 'json'
    ];

    public function getValueByKey($key)
    {
        $setting = $this->where(['key' => $key])->find();
        if ($setting) {
            $value = $setting->value;
            return $value;
        } else {
            return [];
        }
    }

    public function setValueByKey($formData, $key)
    {
        $setting = $this->where(['key' => $key])->findOrEmpty();
        $data = array(
            'key'   => $key,
            'value' => $formData,
        );
        return $setting->save($data);
    }

    public static function get($key)
    {
        $setting = self::where(['key' => $key])->find();
        if ($setting) {
            $value = $setting->value;
            return $value;
        } else {
            return [];
        }
    }
}